package com.example.enterprisemanager.mvvm.fragment.manager;

import android.app.Application;
import android.arch.lifecycle.MutableLiveData;
import android.support.annotation.NonNull;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.enterprisemanager.base.BaseViewModel;
import com.example.enterprisemanager.mvvm.adapter.EnrollAdapter;
import com.example.enterprisemanager.mvvm.ben.User;
import com.example.enterprisemanager.mvvm.fragment.manager.ben.PurchaseGoods;
import com.example.enterprisemanager.mvvm.fragment.manager.ben.SendEnroll;
import com.example.enterprisemanager.util.ToastUtil;
import com.orhanobut.logger.Logger;

import java.util.List;

import cn.bmob.v3.BmobUser;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;

public class EnrollViewModel extends BaseViewModel<EnrollModel> {
    public EnrollAdapter adapter;
    public MutableLiveData<SendEnroll> sendEnroll;


    public EnrollViewModel(@NonNull Application application) {
        super(application, new EnrollModel());
        init();
    }

    private void init() {
        sendEnroll = new MutableLiveData<>();

        adapter = new EnrollAdapter();

        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                Logger.d("onItemChildClick" + position);

                if (!BmobUser.isLogin() ||BmobUser.getCurrentUser(User.class).getPower() > 3) {
                    ToastUtil.show(getApplication(), "您的权限不足，需要的权限为3及以上");
                    return;
                }
                PurchaseGoods purchaseGoods = (PurchaseGoods) adapter.getItem(position);
                sendEnroll.setValue(new SendEnroll(position, purchaseGoods));

            }
        });
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mModel.getHavePurchase(new Consumer<List<PurchaseGoods>>() {
            @Override
            public void accept(List<PurchaseGoods> purchaseGoods) throws Exception {
                adapter.setNewData(purchaseGoods);
            }
        }, new Consumer<Exception>() {
            @Override
            public void accept(Exception e) throws Exception {
                ToastUtil.show(getApplication(), "查询可登记物资失败");
            }
        });
    }

    public void haveEnroll(SendEnroll sendEnroll, int num) {
        if (num <= 0) {
            ToastUtil.show(getApplication(), "输入数据有误");
        } else if (num > sendEnroll.getPurchaseGoods().getNumber()) {
            ToastUtil.show(getApplication(), "超过最大可登记数量");
        } else {

            PurchaseGoods purchaseGoods = sendEnroll.getPurchaseGoods();
            purchaseGoods.setNumber(purchaseGoods.getNumber() - num);
            purchaseGoods.setHaveEnroll(purchaseGoods.getHaveEnroll() + num);

            mModel.haveEnroll(purchaseGoods, new Action() {
                @Override
                public void run() throws Exception {
                    adapter.notifyItemChanged(sendEnroll.getPosition(), purchaseGoods);
                    Logger.d("已提交更新，ui更新成功");
                }
            }, new Consumer<Exception>() {
                @Override
                public void accept(Exception e) throws Exception {
                    Logger.d("提交更新失败");
                }
            });

            mModel.enrollGoods(purchaseGoods, num, new Action() {
                @Override
                public void run() throws Exception {
                    ToastUtil.show(getApplication(), "登记成功，已自动分配唯一id");
                }
            }, new Consumer<Exception>() {
                @Override
                public void accept(Exception e) throws Exception {
                    ToastUtil.show(getApplication(), "登记失败");

                }
            });

        }


    }

}
